package com.test.view;

import com.test.datas.RegistData;
import com.test.datas.RegistIDCheckData;
import com.test.demo.R;
import com.test.http.HttpClientReceiver;
import com.test.http.HttpMethod;
import com.test.http.HttpMethodTag;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;


/**
 * 注册界面
 * @author Harry
 *
 */
public class RegistActivity extends Activity implements OnClickListener,HttpClientReceiver{
	private Handler mHandler = null;
	private static final int NET_LINK_ERROR = 1;
	private static final int ID_CAN_USE = 2;
	private static final int ID_USED = 3;
	private static final int REGIST_SUCC = 4;
	private static final int INIT_MSG = 5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_regist);
		
		setOnClickListener();
		
		setMessageDeal();
	}

	@SuppressLint("HandlerLeak")
	private void setMessageDeal() {
		// TODO Auto-generated method stub
		if(mHandler != null){
			return ;
		}
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch(msg.what){
				case NET_LINK_ERROR:
					showNetLinkError();
					break;
				case ID_CAN_USE:
					showIDCanUser();
					break;
				case ID_USED:
					showIDUsed();
					break;
				case REGIST_SUCC:
					showRegistSucc();
					break;
				}
			}
			
		};
	}

	protected void showRegistSucc() {
		// TODO Auto-generated method stub
		showAlertDialog(this.getResources().getString(R.string.regist_succ));
	}

	protected void showIDUsed() {
		// TODO Auto-generated method stub
		showAlertDialog(this.getResources().getString(R.string.user_id_exit));
	}

	protected void showIDCanUser() {
		// TODO Auto-generated method stub
		showAlertDialog(this.getResources().getString(R.string.user_id_can_user));
	}

	protected void showNetLinkError() {
		// TODO Auto-generated method stub
		showAlertDialog(this.getResources().getString(R.string.net_link_error));
	}
	
	
	protected void showAlertDialog(String msg){
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(R.string.hint);
		builder.setMessage(msg);
		builder.setPositiveButton(R.string.ok, null);
		builder.create().show();
	}

	/**
	 * 初始化按钮点击事件
	 */
	private void setOnClickListener(){
		Button registButton = (Button)this.findViewById(R.id.regist_layout_regist_button);
		if(registButton != null){
			registButton.setOnClickListener(this);
		}
		
		EditText editName = (EditText)this.findViewById(R.id.regist_user_id_editText);
		if(editName != null){
			editName.setOnFocusChangeListener(new OnFocusChangeListener(){

				@Override
				public void onFocusChange(View arg0, boolean arg1) {
					// TODO Auto-generated method stub
					EditText edit = (EditText)arg0;
					if(!edit.hasFocus()){
						HttpMethod.registIDCheck(RegistActivity.this,edit.getText().toString());
					}
				}
				
			});
		}
	}	
	

	/**
	 * 按钮点击事件
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.regist_layout_regist_button:
			doRegistAction();
			break;
		default:
			return;
		}
	}
	
	
	/**
	 * 获取EditText输入内容
	 * @param id ：控件ID
	 * @return ：控件输入内容
	 */
	private String getEditTextFromID(int id){
		EditText editText = (EditText)this.findViewById(id);
		String text = "";
		if(editText != null){
			text = editText.getText().toString();
		}
		return text;
	}
	
	
	/**
	 * 获取输入内容，注册
	 */
	private void doRegistAction(){
		String userID    = getEditTextFromID(R.id.regist_user_id_editText);
		String password  = getEditTextFromID(R.id.regist_password_editText);
		String userName  = getEditTextFromID(R.id.regist_user_name_editText);
		String userEmail = getEditTextFromID(R.id.regist_user_email_editText);
		String userPhone = getEditTextFromID(R.id.regist_user_phone_editText);
		
		doRegistRequest(userID,password,userName,userEmail,userPhone);
		
	}
	
	
	/**
	 * 执行注册流程
	 * @param userID ：用户ID
	 * @param password ：密码
	 * @param userName ：用户名
	 * @param userEmail ： 用户邮箱
	 * @param userPhone ： 用户电话
	 */
	private void doRegistRequest(String userID,String password,String userName,String userEmail,String userPhone){
		Log.d("test","userID:" + userID + "|password:"+password + "|userName:"+userName +"|userEmail:" + userEmail + "|userPhone:"+ userPhone);
	}

	@Override
	public void onHttpResponse(int httpTag, String strJson) {
		// TODO Auto-generated method stub
		switch(httpTag){
		case HttpMethodTag.REGIST_ID_CHECK:
			dealRegistIDCheck(strJson);
			break;
		case HttpMethodTag.REGIST:
			dealRegist(strJson);
			break;
		}
	}
	
	private void dealRegistIDCheck(String strJson){
		RegistIDCheckData data = new RegistIDCheckData();
		int parseResult = data.parseJson(strJson);
		Message msg = new Message();
		msg.what = INIT_MSG;
		switch(parseResult){
		case 1:
			msg.what = ID_CAN_USE;
			break;
		case 0:
			msg.what = ID_USED;
			break;
		case -1:
			msg.what = NET_LINK_ERROR;
			break;
		}
		if(msg.what != INIT_MSG){
			mHandler.sendMessage(msg);
		}
	}
	
	private void dealRegist(String strJson){
		RegistData data = new RegistData();
		int parseResult = data.parseJson(strJson);
		Message msg = new Message();
		msg.what = INIT_MSG;
		switch(parseResult){
		case 1:
			msg.what = REGIST_SUCC;
			break;
		case -1:
			msg.what = NET_LINK_ERROR;
			break;
		}
		if(msg.what != INIT_MSG){
			mHandler.sendMessage(msg);
		}
	}
}
